package com.chl.video;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;

public class TestVideo {

	public static void main(String[] args) {


		extractSecondFrame("C:\\Users\\006006\\Desktop\\185092-874643408_large.mp4", "C:\\Users\\006006\\Desktop\\second.png");

	}

	public static void extractSecondFrame(String videoPath, String outputPath) {
		try (FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoPath)) {
			grabber.start();

			// 定位到第二秒（单位：微秒）
			long targetTime = 2_000_000; // 2秒 = 2,000,000微秒
			grabber.setTimestamp(targetTime);

			// 截取当前帧
			Java2DFrameConverter converter = new Java2DFrameConverter();
			BufferedImage image = converter.convert(grabber.grabImage());

			// 保存为图片
			ImageIO.write(image, "png", new File(outputPath));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
